﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Common;
using DLMemberPointMS.Business;
using DLMemberPointMS.Entity.ViewModel;

namespace DLMemberPointMS.Web.Controllers
{
    public class ShopsController : Controller
    {
        private ShopsService ShopsService = new ShopsService();
        // GET: Shops
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 返回列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult GetShop()
        {
            return View();
        }

        /// <summary>
        /// 显示列表视图
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="ContactName"></param>
        /// <param name="Address"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ActionResult GetShopList(string Name, string ContactName, string Address, int page, int limit)
        {
            var result = ShopsService.Shops(Name, ContactName, Address, page, limit);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //  public ActionResult 
        /// <summary>
        /// 返回添加视图
        /// </summary>
        /// <returns></returns>
        public ActionResult AddShopList()
        {
            var shops = EnumHelper.EnumListDic<ShopTypeEnum>("全部", "0");
            var shopsselectlist = new SelectList(shops, "value", "key");
            ViewData["Enum"] = shopsselectlist;
            return View();
        }
        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetAddShopList(ShopEditViewModel modal)
        {
            var shopmodel = new ShopEditViewModel
            {
                S_Name = modal.S_Name,
                S_Category = modal.S_Category,
                S_ContactName = modal.S_ContactName,
                S_Address = modal.S_Address,
                S_ContactTel = modal.S_ContactTel,
                S_Remark = modal.S_Remark,
                S_CreateTime = DateTime.Now,
                S_IsHasSetAdmin = false,
            };
            var result = ShopsService.AddShops(modal);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="S_ID"></param>
        /// <returns></returns>
        public ActionResult Del(int S_ID)
        {
            var result = ShopsService.DeleShops(S_ID);
            return Json(result, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 返回修改视图
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdeShop(int id)
        {
            using (var db = new DLMPMSModel())
            {

                var shops = EnumHelper.EnumListDic<ShopTypeEnum>("全部", "0");
                var shopsselectlist = new SelectList(shops, "value", "key");
                ViewData["Enum"] = shopsselectlist;
                var shop = db.Shops.Where(c => c.S_ID == id).Select(c => new ShopEditViewModel
                {
                    S_ID = c.S_ID,
                    S_Name = c.S_Name,
                    S_Category = c.S_Category,
                    S_ContactName = c.S_ContactName,
                    S_Address = c.S_Address,
                    S_ContactTel = c.S_ContactTel,
                    S_Remark = c.S_Remark,
                    S_CreateTime = DateTime.Now,
                    S_IsHasSetAdmin = false,
                }).SingleOrDefault();
                return View("AddShopList", shop);
            }
        }
        /// <summary>
        /// 修改视图
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdeShop(ShopEditViewModel model)
        {
            var result = ShopsService.UpdeteShops(model);
            return Json(result, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 判断是否进行分配管理员
        /// </summary>
        /// <param name="S_ID"></param>
        /// <returns></returns>
        public ActionResult PanDuan(int S_ID)
        {
            var result = ShopsService.Assign(S_ID);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 分配管理员视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Assign(int id)
        {
            var result = ShopsService.Assign(id).data;
            return View(result);
        }
        /// <summary>
        /// 分配管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult shopAssign(string Name, int S_ID)
        {
            var result = ShopsService.ShopsAssign(Name, S_ID);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}